package com.cincc.cinccimdemo.ui.conversation.card;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.im.model.dbmodel.FriendInfo;

import java.util.ArrayList;
import java.util.List;

public class PickUserAdapter extends RecyclerView.Adapter<PickUserAdapter.MyViewHolder> {

    Context mContext;
    private final List<FriendInfoUI> mList;

    public PickUserAdapter(Context mContext, List<FriendInfoUI> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setContactList(List<FriendInfo> conversationInfos) {
        this.mList.clear();
        List<FriendInfoUI> temp = new ArrayList<>();
        for (FriendInfo friendInfo : conversationInfos) {
            FriendInfoUI item = ContactUtil.getFriendUI(friendInfo);
            temp.add(item);
        }
        if (!temp.isEmpty()) {
            for (String letter : ContactUtil.LETTER_LIST) {
                List<FriendInfoUI> beanList = ContactUtil.queryByContactHeader(temp, letter);
                this.mList.addAll(beanList);
            }
            notifyDataSetChanged();
        }
    }

    public List<FriendInfoUI> getList() {
        return mList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_contact_my_friend_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        FriendInfoUI model = mList.get(position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;

        myViewHolder.nameText.setText(model.nickName);

        myViewHolder.firstTv.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.VISIBLE : View.GONE);
        myViewHolder.avatar.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.GONE : View.VISIBLE);
        if (TextUtils.isEmpty(model.protraitUrl)) {
            myViewHolder.firstTv.setText(model.nickName.substring(0, 1));
        } else {
            Glide.with(mContext).load(model.protraitUrl).placeholder(R.mipmap.avatar_def)
                    .transforms(new CenterCrop(), new RoundedCorners(15))
                    .into(myViewHolder.avatar);
        }

        if (position == ContactUtil.getPositionForSection(position, 1, mList)) {
            myViewHolder.headerTv.setVisibility(View.VISIBLE);
            myViewHolder.headerTv.setText(model.getLetterName());
        } else {
            myViewHolder.headerTv.setVisibility(View.GONE);
        }

        myViewHolder.itemView.setOnClickListener(v -> {
            iOnDataClickItem.onClick(position);
        });
    }

    private OnItemClickListener iOnDataClickItem;

    public void setOnDataClickItem(OnItemClickListener iOnClickItem) {
        this.iOnDataClickItem = iOnClickItem;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTv;
        TextView nameText;
        TextView headerTv;
        ImageView avatar;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstTv = itemView.findViewById(R.id.first_letter_tv);
            nameText = itemView.findViewById(R.id.mText_item_contact_name);
            headerTv = itemView.findViewById(R.id.mText_list_item_header);
            avatar = itemView.findViewById(R.id.img_avatar);
        }
    }
}
